package com.swing.border;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;

/**
 * @author zsj
 * @date 2023/4/17 15:15
 */
public class BorderDemo {

    JFrame jFrame = new JFrame("测试");

    void init(){
        //对窗口设置卡片风格 并且设置2行四列
        jFrame.setLayout(new GridLayout(2,4));

        //在网格中添加不同的JPanel组件,并且设置边框和内容

        //创建bevelBorder
        Border bevelBorder =
                BorderFactory.createBevelBorder(BevelBorder.RAISED
                        ,Color.RED,Color.GREEN,Color.BLUE,Color.GRAY);
        //风格(突起或陷下) 风格外边颜色 风格内边颜色  阴影外边颜色 阴影内边颜色
        jFrame.add(getJPanelWithBorder(bevelBorder,"bevelBorder"));

        //创建lineBorder
        Border lineBorder =
                BorderFactory.createLineBorder(Color.ORANGE,
                        10);//颜色与厚度
        jFrame.add(getJPanelWithBorder(lineBorder,"lineBorder"));

        //创建emptyBorder
        Border emptyBorder =
                BorderFactory
                        .createEmptyBorder(10,5,20,30);
                        //上左内右边距设置
        jFrame.add(getJPanelWithBorder(emptyBorder,"emptyBorder"));

        //创建etchedBorder
        Border etchedBorder = BorderFactory
                .createEtchedBorder(
                        EtchedBorder.RAISED,
                        Color.RED, Color.GREEN);//风格（突起和下陷） 高亮颜色 阴影颜色
        jFrame.add(getJPanelWithBorder(etchedBorder,"etchedBorder"));

        //创建titleBorder
        TitledBorder titledBorder =
                new TitledBorder(
                        new LineBorder(Color.ORANGE,10),
                        "测试标题");
        jFrame.add(getJPanelWithBorder(titledBorder,"测试标题"));

        //创建MatteBorder
        MatteBorder matteBorder = new MatteBorder(10, 5, 20, 10, Color.GREEN);
        jFrame.add(getJPanelWithBorder(matteBorder,"matteBorder"));

        //创建compoundBorder
        CompoundBorder compoundBorder = new CompoundBorder(titledBorder,
                new LineBorder(Color.RED,10));
        jFrame.add(getJPanelWithBorder(compoundBorder,"compoundBorder"));


        //设置窗口最佳大小
        jFrame.pack();
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public JPanel getJPanelWithBorder(Border border,String content){
        JPanel jPanel = new JPanel();
        jPanel.add(new JLabel(content));
        jPanel.setBorder(border);
        return jPanel;
    }


    public static void main(String[] args) {
        new BorderDemo().init();
    }
}
